<?php
require_once('classes/config.inc.php');
require_once(DIR_CLASS. 'Session.php');
require_once(DIR_CLASS. 'Database.php');
include(DIR_CLASS. 'TemplateParser.php');

define('JOBS_PER_PAGE','10');

$session = new UserSession();
$session->open();

$db = new MYSQLDatabase(DB_SERVER,DB_USER,DB_PASS,DB_DATABASE);
try {
		$db->connect();
}catch (Exception $e){
	echo $e->getMessage();
	die();
}

$captcha = new Captcha();
$captcha->open();

if($session->check())
	$menu = "<a href=\"profile.php\">Profil</a> / <a href=\"login.php?act=logout\">Logga ut</a>[". substr($session->getEmail(),0,12) ."] / <a href=\"\">Karta &ouml;ver hemsidan</a>";
else
	$menu = "<a href=\"register.php\">Registrera dig</a> / <a href=\"login.php\">Logga in</a> / <a href=\"sitemap.php\">Karta &ouml;ver hemsidan</a>";

// SUBSCRIBES USER FOR JOB
if(isset($_GET["sub"]) && is_numeric($_GET["sub"]) && $_GET["sub"]>0 && $captcha->check($_GET["captcha"]) && $session->check()){
	$sql = "SELECT * FROM ". TABLE_JOBS ." WHERE(Job_id='".$_GET["sub"]."') LIMIT 0,1";
	$link = $db->query($sql);
	if(mysql_num_rows($link)>0){
		$sql = "INSERT INTO ". TABLE_APPLY ." VALUES('".$_GET["sub"]."','".$session->getId()."')";
		$link = $db->query($sql);
		if($link){

			header('Location: index.php');
			die();
		}
	}
}
// SHOW ALL JOBS FROM CATEGORY
else if(isset($_GET["cat"]) && is_numeric($_GET["cat"]) && $_GET["cat"]>0){
	// GET JOBS in $jobs
	$job_list="";
	$p = "0";
	
	//GET PAGE NUMBER
	if(isset($_GET["p"]) && is_numeric($_GET["p"]) && $_GET["p"]>0){
		$p = $_GET["p"];
	}
	$sql = "SELECT * FROM ". TABLE_JOBS .",".TABLE_USERS." WHERE(Cat_id='".$_GET["cat"]."' AND Job.finished='0' AND Job.User_id=User.id) ORDER BY `Job`.`deadline` ASC  LIMIT ". $p*JOBS_PER_PAGE ." , ".JOBS_PER_PAGE;
	$link = $db->query($sql);
	if(mysql_num_rows($link)>0){
		while($res = mysql_fetch_array($link)){
			$job_list .= "<tr>\n
							<td><a href=\"?job=".$res["id"]."\">".$res["title"]."</a></td>\n
							<td>".$res["desc"]."</td>\n
							<td>".$res["deadline"]."</td>\n
							<td>".$res["fee"]."</td>\n
							<td>".$res["add_date"]."</td>\n
							<td>".$res["name"] ." ".$res["family_name"]."</td>\n
							<td><a href=\"?\"
							</tr>\n";
		}
	}
	else{
		
	}
	
	// GET CATEGORIE DETAILS.
	$sql = "SELECT COUNT(Job.id) as count,Cat.title,Cat.desc FROM ". TABLE_CAT .",". TABLE_JOBS ." WHERE(Job.Cat_id='".$_GET["cat"]."' AND Job.Cat_id=Cat.id) LIMIT 0,1";
	$link = $db->query($sql);
	if(mysql_num_rows($link)>0){
		while($res = mysql_fetch_array($link)){
			$cat_count = $res["count"];
			$cat_title = $res["title"];
		}
	}
	else{
		
	}
	
	// MAKE PAGES
	$total_pages = (int) ($cat_count/JOBS_PER_PAGE)+1;
	$current_page = $p+1;
	$next_page = "<a href=\"?cat=".$_GET["cat"]."&p=". ($p+1) ."\">". ($current_page+1) ."</a>";
	$prev_page = "<a href=\"?cat=".$_GET["cat"]."&p=". ($p-1) ."\">". ($current_page-1) ."</a>";
	
	if($p==0){
		$prev_page="";
	}
	if($p==($total_pages-1)){
		$next_page="";
	}
	$template_array = array(
		"MENU" =>$menu,
		"CAT_TITLE" => $cat_title,
		"CAT_COUNT" => $cat_count,
		"JOB_LIST" => $job_list,
		"PREV_PAGE" => $prev_page,
		"NEXT_PAGE" => $next_page,
		"CURRENT_PAGE" => $current_page,
		"TOTAL_PAGES" => $total_pages
	);
		
	$template = new TemplateParser();
	$template->loadfile("templates/job_list.tpl");
	$template->assignVars($template_array);
	$template->output();
}
// SHOW ONE SPECIFIC JOB.
else if(isset($_GET["job"]) && is_numeric($_GET["job"]) && $_GET["job"]>0){
	// GET JOB
	$sql = "SELECT *,Cat.title as ctitle,Cat.id as category FROM ".TABLE_JOBS.", ".TABLE_CAT.", ".TABLE_USERS." WHERE(Job.id='".$_GET["job"]."' AND Job.Cat_id=Cat.id AND Job.User_id=Job.id) LIMIT 0,1";
	$link = $db->query($sql);
	
	if(mysql_num_rows($link)>0){
		$res = mysql_fetch_array($link);
		
		if($session->check())
				$email="value=\"".$session->getEmail()."\" readonly=\"readonly\"";
		else
			$email="";
				
		
		$captcha->open();
		$captcha->save();
		$template_array = array(
			"MENU" =>$menu,
			"JOB_ID" => $res["id"],
			"JOB_TITLE" => $res["title"],
			"JOB_DESC" => $res["description"],
			"JOB_ADD" => $res["add_date"],
			"FEE" => $res["fee"],
			"CAT_ID" => $res["category"],
			"JOB_DEAD" => $res["deadline"],
			"AUTHOR" => $res["family_name"]." ".$res["name"],
			"AUTHOR_L" => $res["User_id"],
			"CAPTCHA" => $captcha->getQuestion(),
			"EMAIL" => $email,
			"MESS" => "",
			"ERROR" => "",
			"CAT_TITEL" => $res["ctitle"]
		);
		
		$template = new TemplateParser();
		$template->loadfile("templates/job.tpl");
		$template->assignVars($template_array);
		$template->output();
	}
	else{
		header('Location: index.php');
		die();
	}
}
// SHOW CATEGORIES
else{
	$sql = "SELECT COUNT(Job.id) as count,Cat.title,Cat.desc,Cat.id FROM ". TABLE_JOBS .",". TABLE_CAT .
	" WHERE (Job.Cat_id = Cat.id)
	GROUP BY Cat.id
	ORDER BY `Cat`.`title` ASC";	
	$cat= "";
	try {
		$db->connect();
		$link = $db->query($sql);
		if(mysql_num_rows($link)>0){
			while($res = mysql_fetch_array($link)){
				$cat .= "<li><a href=\"?cat=".$res["id"]."\" title=\"".$res["desc"]."\">". $res["title"] ." (".$res["count"].")</a>".$res["desc"]."</li>\n";
			}
		}
		else{
			
		}
	}catch (Exception $e){
		echo $e->getMessage();
	}
	
	$template_array = array(
		"MENU" =>$menu,
		"CAT_LIST" => $cat
	);
		
	$template = new TemplateParser();
	$template->loadfile("templates/cat_list.tpl");
	$template->assignVars($template_array);
	$template->output();
}
?>